﻿#include "forward_node.h"
#include <QDebug>

Forward_Node::Forward_Node(const QJsonObject &info, QObject *parent): QThread(parent)
{
    qDebug()<<"new Node";
    m_forwardInfo = info;
}

Forward_Node::~Forward_Node()
{
}

void Forward_Node::run()
{
    qDebug()<<"任务开始";
    m_forwardSock = new QSslSocket();
    connect(m_forwardSock, &QSslSocket::preSharedKeyAuthenticationRequired,
            this, [&](QSslPreSharedKeyAuthenticator *authenticator){
        authenticator->setIdentity("Test");
        const QByteArray key = "Hello";
        authenticator->setPreSharedKey(key);
    });
    m_appSock = new QTcpSocket();
    qDebug()<<m_forwardInfo;
    QString forward_ip = m_forwardInfo["forwardHost"].toString(); //服务器IP
    QString app_ip = m_forwardInfo["appHost"].toString();         //需转发的APP的IP
    quint16 forward_port = m_forwardInfo["forwardPort"].toString().toInt();   //服务器端口
    quint16 app_port = m_forwardInfo["appPort"].toString().toInt();           //需转发的APP的端口
    int forward_timeout = Forward_Config::GetTimeoutSecond();//两个超时时间
    int app_timeout = Forward_Config::GetTimeoutSecond();
    qDebug()<<"forward_timeout "<<forward_timeout;
    qDebug()<<"app_timeout "<<app_timeout;
    
    QFile file("./ssl/server.key");
    file.open(QIODevice::ReadOnly);
    
    QSslConfiguration configuration;
    //等待证书，但不需要有效
    configuration.setPeerVerifyMode(QSslSocket::VerifyNone);
    m_forwardSock->setSslConfiguration(configuration);
    m_forwardSock->connectToHostEncrypted(forward_ip,forward_port);
    if(!m_forwardSock->waitForEncrypted(forward_timeout))
    {
        qDebug()<<"与服务器握手超时";
        qDebug()<<m_forwardSock->errorString();
        exit();  //连接不成功直接退出了
        return;
    }
    
    //等待服务器发送数据
    while (!m_forwardSock->waitForReadyRead(forward_timeout)) {}
    
    qDebug()<<"与服务器连接成功";
    qDebug()<<"ip "<<app_ip;
    qDebug()<<"port "<<app_port;
    m_appSock->connectToHost((app_ip),app_port);
    if(!m_appSock->waitForConnected(app_timeout))
    {
        qDebug()<<"与app连接失败";
        qDebug()<<m_appSock->errorString();
        exit();
        return;
    }
    
    m_appSock->write(m_forwardSock->readAll());  //手动接收一次
    
    connect(m_appSock,&QTcpSocket::readyRead,this,&Forward_Node::forwardDataToTarget,Qt::DirectConnection);
    connect(m_forwardSock,&QSslSocket::readyRead,this,&Forward_Node::forwardDataToSource,Qt::DirectConnection);
    
    connect(m_appSock,&QTcpSocket::disconnected,this,&Forward_Node::leave,Qt::DirectConnection);
    connect(m_forwardSock,&QSslSocket::disconnected,this,&Forward_Node::leave,Qt::DirectConnection);
    
    this->exec();
}

void Forward_Node::stop(Forward_Node *node)
{
    if(node == this)
    {
        this->leave();
    }
}

void Forward_Node::forwardDataToSource()
{
    QByteArray buffer = m_forwardSock->readAll();
    m_appSock->write(buffer);
}

void Forward_Node::leave()
{
    qDebug()<<"任务释放";
    if(m_forwardSock != nullptr)
    {
        m_forwardSock->disconnectFromHost();
        m_forwardSock->close();
        m_forwardSock->deleteLater();
    }
    if(m_appSock != nullptr)
    {
        m_appSock->disconnectFromHost();
        m_appSock->close();
        m_appSock->deleteLater();
    }
    this->exit();
}

void Forward_Node::forwardDataToTarget()
{
    QByteArray buffer = m_appSock->readAll();
    qDebug()<<"来自服务器的数据";
    qDebug()<<buffer;
    m_forwardSock->write(buffer);
}
